package com.itheima.app.manager;

import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.mongo.FocusUser;
import com.itheima.service.mongo.FocusUserService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class FocusUserManager {

    @DubboReference
    private FocusUserService focusUserService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //关注
    public void saveFocusUser(Long focusUserId){
        Long userId = UserHolder.get().getId();
        FocusUser focusUser = new FocusUser();
        focusUser.setCreated(System.currentTimeMillis());
        focusUser.setFocusUserId(focusUserId);
        focusUser.setUserId(userId);
        //存到数据库
        focusUserService.saveFocusUserService(focusUser);

        //存到redis
        stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.FOCUS_USER, userId,focusUserId),"1");
    }

    //取消关注
    public void removeFocusUser(Long focusUserId) {
        Long userId = UserHolder.get().getId();
        focusUserService.removeFocusUserService(userId,focusUserId);
        stringRedisTemplate.delete(StrUtil.format(ConstantUtil.FOCUS_USER,userId,focusUserId));
    }



}
